package cn.uniquesoft.aop;

import java.util.Date;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import cn.uniquesoft.manager.ClientManager;
import cn.uniquesoft.vo.UserVO;
import cn.uniquesoft.vo.base.BaseVO;

/**
 * @创建人 zyx
 * @创建时间 2017年6月26日 下午6:59:18
 * @描述 aop处理，新增，修改
 */
@Aspect
@Component
public class ControllerInterceptor {
    // 匹配cn.uniquesoft.controller包及其子包下的所有类的所有方法
    @Pointcut("execution(* cn.uniquesoft.controller..*.*(..))")
    public void executeService() {

    }

    /**
     * 前置通知，方法调用前被调用
     *
     * @param joinPoint
     */
    @Before("executeService()")
    public void doBeforeAdvice(JoinPoint joinPoint){
        // 通知的签名
        Signature signature = joinPoint.getSignature();
        // 代理的是哪一个方法
        String methodName = signature.getName();

        if ("save".equals(methodName) || "newSave".equals(methodName)) {
            if (joinPoint.getArgs()[0] instanceof BaseVO) {
                UserVO userVO = null;
                try {
                	userVO = ClientManager.getInstance().getClient().getUser();
				} catch (Exception e) {
					
				}
                
                BaseVO baseVO = (BaseVO) joinPoint.getArgs()[0];
                Date date = new Date();
                if(userVO!=null){
                	baseVO.setIcreator(userVO.getIid());
                    baseVO.setCcreator(userVO.getCname());
                    baseVO.setImodify(userVO.getIid());
                    baseVO.setCmodify(userVO.getCname());
                }
                
                baseVO.setDcreator(date);
               
                baseVO.setDmodify(date);
            }
        }

        if ("edit".equals(methodName) || "newEdit".equals(methodName)) {
            if (joinPoint.getArgs()[0] instanceof BaseVO) {
                UserVO userVO = null;
                try {
                	userVO = ClientManager.getInstance().getClient().getUser();
				} catch (Exception e) {
					// TODO: handle exception
				}
                
                BaseVO baseVO = (BaseVO) joinPoint.getArgs()[0];
                Date date = new Date();
                baseVO.setImodify(userVO.getIid());
                baseVO.setCmodify(userVO.getCname());
                baseVO.setDmodify(date);
            }
        }

    }

   

}
